package com.superjson.showpro.service.impl;

import com.superjson.showpro.base.GenericServiceImpl;
import com.superjson.showpro.dao.CommentDao;
import com.superjson.showpro.entity.Comment;
import com.superjson.showpro.service.ArticleCatalogService;
import com.superjson.showpro.service.CommentService;
import com.superjson.showpro.service.UserService;
import com.superjson.showpro.shared.enums.CommentTypeEnum;
import com.superjson.showpro.shared.result.Result;
import com.superjson.showpro.shared.result.ResultUtil;
import com.superjson.showpro.shared.util.CommonUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.beantools.util.ObjectUtil.isNull;
import static com.superjson.showpro.shared.enums.CommentTypeEnum.COMMENT_TYPE_COMMENT;
import static com.superjson.showpro.shared.util.CommonUtil.getQuillContent;
import static com.superjson.showpro.shared.util.CommonUtil.ifTrueThrow;

@Service
public class CommentServiceImpl extends GenericServiceImpl<Comment> implements CommentService {

    private CommentDao commentDao;
    @Autowired
    private ArticleCatalogService articleCatalogService;
    @Autowired
    private UserService userService;

    @Autowired
    public CommentServiceImpl(CommentDao commentDao) {
        super(commentDao);
        this.commentDao = commentDao;
    }

    @Override
    public Result saveComment(String content, Long articleId, Long userId, CommentTypeEnum commentType) {
        ifTrueThrow(isNull(articleId) || articleId <= 0, "文章id必传，且不能小于0");
        ifTrueThrow(isNull(userId) || userId <= 0, "文章id必传，且不能小于0");
        Comment comment = new Comment();
        comment.setArticleCatalog(articleCatalogService.get(articleId).getResultData());
        comment.setUser(userService.get(userId).getResultData());
        comment.setCommentType(isNull(comment.getCommentType()) ? COMMENT_TYPE_COMMENT : comment.getCommentType());
        comment.setContent(getQuillContent(content));
        commentDao.save(comment);
        return ResultUtil.buildSuccess();
    }

    @Override
    public Result<List<Comment>> listComment(Long userId, Long articleId) {
        return ResultUtil.buildSuccess(commentDao.listComment(userId, articleId));
    }

    @Override
    public Result<List<Comment>> listComment(Long articleId) {
        return ResultUtil.buildSuccess(commentDao.listComment(articleId));
    }
}